$(function() {
	layui.use(['form', 'layer'], function() {
		var form = layui.form;
		form.render();
		var layer = layui.layer;

		//获取列表的函数
		function find_menu_list() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/index/selectEnumRoleMenu",
				async: true,
				"success": function(result) {
					console.log(result);
					build_menu_list(result);
				}
			});
		} 
		BaseUtil.ajaxSetup();
		function build_menu_list(result) {
			var menuList = result.data;
			$.each(menuList, function(index,item) {
				var parentDiv=$("<div></div>").addClass("layui-form-item");
				var smDiv=$("<div></div>").addClass("layui-input-block");
				var parentCheckBox=$("<input type='checkbox' class='parentCheckBox' lay-filter='parentCheckBox' value='"+item.code+"' title='"+item.name+"'/>");
				smDiv.append(parentCheckBox);
				var smCheckBoxAttr=item.roleValue.split(",");
				for(var i=0;i<smCheckBoxAttr.length;i++){
					var smCheckBox=$("<input type='checkbox' class='childrenCheckBox' lay-filter='childrenCheckBox' title='"+smCheckBoxAttr[i]+"'/>");
					smDiv.append(smCheckBox);
				}
				parentDiv.append(smDiv);
				$(".menu_list").append(parentDiv);
			});
			form.render();
		}
		


		//表单的绑定事件
		form.on("checkbox(parentCheckBox)",function(data){
			if(data.elem.checked){
				$(this).nextAll("input[type='checkbox']").prop("checked",true);
			}else{
				$(this).nextAll("input[type='checkbox']").prop("checked",false);
			}
			form.render();
		});
		//反选
		form.on("checkbox(childrenCheckBox)",function(data){
			
			var checkLength = 0; 
			$(this).parent().children("input[class='childrenCheckBox']").each(function(){
				if($(this).is(":checked")){
					checkLength++
				}
			});
			if(checkLength>0){
				$(this).parent().children("input[class='parentCheckBox']").prop("checked",true);
			}else{
				$(this).parent().children("input[class='parentCheckBox']").prop("checked",false);
			}
				form.render();
		});
		
		$(".addRole").click(function(){
				add_menu_name();
				var token=uuid(16,16);
				$("#token").val(token);
				
		});
		
		form.on("submit(addRole)",function(data){
			var params=data.field;
			add_Role(params);
			return false;
		});
		
		function add_Role(params){
			$.ajax({
				type:"post",
				url:"http://localhost:8080/ssmService/role/insertRoleVo",
				async:true,
				data:params,
				"success":function(result){
					if(result.resultCode==0){
						layer.msg("添加成功!",{
							icon:1,
							time:1000
						});
					}
					parent.location.reload();
				}
			});
		}
		
		function add_menu_name(){
				var index=0;
			$(".parentCheckBox").each(function(){
				if($(this).is(":checked")){
				$(this).attr("name","listRolePermission["+index+"].code");
				var value="";
				$(this).nextAll("input[class='childrenCheckBox']").each(function(){
					if($(this).is(":checked")){
						value+="1";
					}else{
						value+="0";
					}
				});
				var hidden=$("<input type='hidden' name='listRolePermission["+index+"].permission_value' value='"+value+"'>");
				$(this).before(hidden);
				index++;
				}
			});
		}
		find_menu_list();
		
	
	});
});